package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.file.ConfigManager;
import com.fr.base.file.FaceManager;
import com.fr.privilege.authentication.Authentication;
import com.fr.privilege.authentication.AuthenticationFactory;
import com.fr.web.core.WebUtils;
import com.fr.web.face.FaceNode;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FSService extends NoSessionIDOPService
{
  private static FSService FSSERVICE = new FSService();

  public static FSService getInstance()
  {
    return FSSERVICE;
  }

  public boolean accept(String paramString)
  {
    return "fs".equals(paramString);
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    FaceManager localFaceManager = FRContext.getFaceManager();
    if (localFaceManager != null)
      try
      {
        int i;
        Authentication localAuthentication = AuthenticationFactory.extractAuthentication(paramHttpServletRequest);
        FaceNode localFaceNode1 = localFaceManager.getRootFaceNode();
        Map localMap = WebUtils.createTemplateMap(paramHttpServletRequest);
        if ((localAuthentication != null) && (localAuthentication.getPrincipal() != null))
        {
          localMap.put("username", Inter.getLocText("Username") + ":" + localAuthentication.getPrincipal().toString());
          localMap.put("loginout", createLoginoutHtml(localMap, paramHttpServletRequest, paramHttpServletResponse));
        }
        else
        {
          localMap.put("username", Inter.getLocText("Username") + ":" + "administrator");
        }
        localMap.put("charset", FRContext.getConfigManager().getServerCharset());
        JSONArray localJSONArray = new JSONArray();
        if (localFaceNode1.getChildCount() > 0)
          for (i = 0; i < localFaceNode1.getChildCount(); ++i)
          {
            FaceNode localFaceNode2 = localFaceNode1.getChild(i);
            JSONObject localJSONObject = localFaceNode2.toJSONConfig(paramHttpServletRequest, localAuthentication, localFaceManager, "1", i + 1);
            if (localJSONObject != null)
              localJSONArray.put(localJSONObject);
          }
        if (localJSONArray.length() == 0)
          localMap.put("warning", "warning");
        localMap.put("trees", localJSONArray.toString());
        if ((localFaceManager.isShowHomePage()) && (localFaceManager.getHomeURLPath() != null))
          localMap.put("home", localFaceManager.getHomeURLPath());
        if (localFaceManager.getHomeTitle() != null)
          localMap.put("homeTitle", localFaceManager.getHomeTitle());
        localMap.put("fsdirectory", Inter.getLocText("Folder"));
        localMap.put("fscontent", Inter.getLocText("Content"));
        TemplateUtils.dealWithTemplate("com/fr/web/face/face.html", paramHttpServletResponse, localMap);
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, "Error happens when deal with op=face", localException);
      }
  }

  private static String createLoginoutHtml(Map paramMap, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("<a href='");
    localStringBuffer.append("${servletURL}?op=auth_logout'>");
    localStringBuffer.append(Inter.getLocText("Logout"));
    localStringBuffer.append("</a>");
    return TemplateUtils.render(localStringBuffer.toString(), paramMap);
  }
}